
/**
  ******************************************************************************
  * Copyright 2021 The Microbee Authors. All Rights Reserved.
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  * 
  * http://www.apache.org/licenses/LICENSE-2.0
  * 
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  * 
  * @file       startup.c
  * @author     baiyang
  * @date       2023-2-17
  ******************************************************************************
  */

/*----------------------------------include-----------------------------------*/
#include <rthw.h>
#include <rtthread.h>
/*-----------------------------------macro------------------------------------*/
#ifndef RT_MAIN_THREAD_STACK_SIZE
#define RT_MAIN_THREAD_STACK_SIZE     2048
#endif /* RT_MAIN_THREAD_STACK_SIZE */
#ifndef RT_MAIN_THREAD_PRIORITY
#define RT_MAIN_THREAD_PRIORITY       (RT_THREAD_PRIORITY_MAX / 3)
#endif /* RT_MAIN_THREAD_PRIORITY */
/*----------------------------------typedef-----------------------------------*/

/*---------------------------------prototype----------------------------------*/
void mb_application_init(void);
void rt_hw_board_init(void);
int rtthread_startup(void);
/*----------------------------------variable----------------------------------*/

/*-------------------------------------os-------------------------------------*/

/*----------------------------------function----------------------------------*/
#ifdef RT_USING_COMPONENTS_INIT
/*
 * Components Initialization will initialize some driver and components as following
 * order:
 * mbi_start          --> 7
 * BOARD_PREV_EXPORT  --> 8
 * BOARD_EXPORT       --> 9
 * COMPONENT_EXPORT   --> A
 * CONSTRUCTOR_EXPORT --> B
 * APP_EXPORT         --> C
 * APP2_EXPORT        --> D
 *
 * mbi_end            --> D.end
 *
 * These automatically initialization, the driver or component initial function must
 * be defined with:
 * MB_INIT_BOARD_PREV_EXPORT(fn);
 * MB_INIT_BOARD_EXPORT(fn);
 * ...
 * MB_INIT_APP2_EXPORT(fn);
 * etc.
 */

static int mbi_start(void)
{
    return 0;
}
INIT_EXPORT(mbi_start, "7");

static int mbi_end(void)
{
    return 0;
}
INIT_EXPORT(mbi_end, "D.end");

/**
 * @brief  RT-Thread Components Initialization.
 */
void mb_components_init(void)
{
#if RT_DEBUG_INIT
    int result;
    const struct rt_init_desc *desc;

    rt_kprintf("do components initialization.\n");
    for (desc = &__rt_init_desc_mbi_start; desc < &__rt_init_desc_mbi_end; desc ++)
    {
        rt_kprintf("initialize %s", desc->fn_name);
        result = desc->fn();
        rt_kprintf(":%d done\n", result);
    }
#else
    volatile const init_fn_t *fn_ptr;

    for (fn_ptr = &__rt_init_mbi_start; fn_ptr < &__rt_init_mbi_end; fn_ptr ++)
    {
        (*fn_ptr)();
    }
#endif /* RT_DEBUG_INIT */
}
#endif

/**
 * This function will show the version of rt-thread rtos
 */
void mb_show_version(void)
{
    rt_kprintf("\n");
    rt_kprintf("    __  ____                 __\n");
    rt_kprintf("   /  |/  (_)_____________  / /_  ___  ___ \n");
    rt_kprintf("  / /|_/ / / ___/ ___/ __ \\/ __ \\/ _ \\/ _ \\ \n");
    rt_kprintf(" / /  / / / /__/ /  / /_/ / /_/ /  __/  __/ \n");
    rt_kprintf("/_/  /_/_/\\___/_/   \\____/_.___/\\___/\\___/ \n");
    rt_kprintf("build %s %s\n", __DATE__, __TIME__);
    rt_kprintf("2022 - 2023 Copyright by Microbee team\n");
}

#ifndef RT_USING_HEAP
/* if there is not enable heap, we should use static thread and stack. */
ALIGN(RT_ALIGN_SIZE)
static rt_uint8_t main_thread_stack[RT_MAIN_THREAD_STACK_SIZE];
struct rt_thread main_thread;
#endif /* RT_USING_HEAP */

/**
 * @brief  The system main thread. In this thread will call the rt_components_init()
 *         for initialization of RT-Thread Components and call the user's programming
 *         entry main().
 */
void main_thread_entry(void *parameter)
{
#ifdef RT_USING_COMPONENTS_INIT
    /* RT-Thread components initialization */
    rt_components_init();
#endif /* RT_USING_COMPONENTS_INIT */

    mb_components_init();

#ifdef RT_USING_SMP
    rt_hw_secondary_cpu_up();
#endif /* RT_USING_SMP */
    /* invoke system main function */
}

/**
 * @brief  This function will create and start the main thread, but this thread
 *         will not run until the scheduler starts.
 */
void mb_application_init(void)
{
    rt_thread_t tid;

#ifdef RT_USING_HEAP
    tid = rt_thread_create("main", main_thread_entry, RT_NULL,
                           RT_MAIN_THREAD_STACK_SIZE, RT_MAIN_THREAD_PRIORITY, 20);
    RT_ASSERT(tid != RT_NULL);
#else
    rt_err_t result;

    tid = &main_thread;
    result = rt_thread_init(tid, "main", main_thread_entry, RT_NULL,
                            main_thread_stack, sizeof(main_thread_stack), RT_MAIN_THREAD_PRIORITY, 20);
    RT_ASSERT(result == RT_EOK);

    /* if not define RT_USING_HEAP, using to eliminate the warning */
    (void)result;
#endif /* RT_USING_HEAP */

    rt_thread_startup(tid);
}

/**
 * @brief  This function will call all levels of initialization functions to complete
 *         the initialization of the system, and finally start the scheduler.
 */
int rtthread_startup(void)
{
    rt_hw_interrupt_disable();

    /* board level initialization
     * NOTE: please initialize heap inside board initialization.
     */
    rt_hw_board_init();

    /* show RT-Thread version */
    mb_show_version();

    /* timer system initialization */
    rt_system_timer_init();

    /* scheduler system initialization */
    rt_system_scheduler_init();

#ifdef RT_USING_SIGNALS
    /* signal system initialization */
    rt_system_signal_init();
#endif /* RT_USING_SIGNALS */

    /* create init_thread */
    mb_application_init();

    /* timer thread initialization */
    rt_system_timer_thread_init();

    /* idle thread initialization */
    rt_thread_idle_init();

#ifdef RT_USING_SMP
    rt_hw_spin_lock(&_cpus_lock);
#endif /* RT_USING_SMP */

    /* start scheduler */
    rt_system_scheduler_start();

    /* never reach here */
    return 0;
}


#ifdef __ARMCC_VERSION
/* main function */
int main(void)
{
    rtthread_startup();
    return 0;
}
#elif defined(__GNUC__)
/* Add -eentry to arm-none-eabi-gcc argument */
int entry(void)
{
    rtthread_startup();
    return 0;
}
#endif

#if defined(__ARMCC_VERSION)
extern void $Super$$__cpp_initialize__aeabi_(void);
/* we need to change the cpp_initialize order */
RT_WEAK void $Sub$$__cpp_initialize__aeabi_(void)
{
    /* empty */
}
#elif defined(__GNUC__) && !defined(__CS_SOURCERYGXX_MAJ__)
/* The _init()/_fini() routines has been defined in codesourcery g++ lite */
RT_WEAK void _init()
{
}

RT_WEAK void _fini()
{
}

RT_WEAK void *__dso_handle = 0;

#endif
/*------------------------------------test------------------------------------*/


